# ufw-init-functions: functions used by ufw-init and distribution initscripts
#
# Copyright 2008-2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -e
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
for s in "/etc/default/ufw" "/etc/ufw/ufw.conf" ; do
if [ -s "$s" ]; then
. "$s"
else
echo "Could not find $s (aborting)"
exit 1
fi
done
RULES_PATH="/etc/ufw"
USER_PATH="/lib/ufw"
flush_builtins() {
error=""
execs="iptables"
if ip6tables -L INPUT -n >/dev/null 2>&1; then
execs="$execs ip6tables"
fi
for exe in $execs
do
$exe -F || error="yes"
$exe -X || error="yes"
$exe -P INPUT ACCEPT || error="yes"
$exe -P OUTPUT ACCEPT || error="yes"
$exe -P FORWARD ACCEPT || error="yes"
# now handle the mangle table
if $exe -t mangle -L -n >/dev/null 2>&1; then
for i in INPUT OUTPUT FORWARD PREROUTING POSTROUTING ; do
$exe -t mangle -F $i || error="yes"
$exe -t mangle -P $i ACCEPT || error="yes"
done
fi
done
# now handle the nat table
if iptables -t nat -L -n >/dev/null 2>&1; then
for i in OUTPUT PREROUTING POSTROUTING ; do
iptables -t nat -F $i || error="yes"
iptables -t nat -P $i ACCEPT || error="yes"
done
fi
if [ "$error" = "yes" ]; then
return 1
fi
}
chains_command() {
flag="$1"
type=""
exe="iptables"
if [ "$2" = "6" ]; then
type="$2"
exe="ip6tables"
fi
for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-limit-accept ufw$type-user-limit ufw$type-reject-input ufw$type-after-logging-input ufw$type-after-input ufw$type-user-input ufw$type-before-input ufw$type-before-logging-input ufw$type-reject-forward ufw$type-after-logging-forward ufw$type-after-forward ufw$type-user-logging-forward ufw$type-user-forward ufw$type-before-forward ufw$type-before-logging-forward ufw$type-track-output ufw$type-track-input ufw$type-reject-output ufw$type-after-logging-output ufw$type-after-output ufw$type-user-logging-output ufw$type-user-output ufw$type-before-output ufw$type-before-logging-output; do
if [ "$UFW_INIT_DEBUG" = "yes" ]; then
echo "$exe $flag $c" >&2
$exe $flag $c || true
else
$exe $flag $c 2>/dev/null || true
fi
done
}
delete_chains() {
chains_command -F $1
chains_command -Z $1
# Delete the secondary chains to reduce clutter, but keep the primary ones
# so that the primary chains don't leave the built-in chains just to come
# back later in a different place. This means that some (empty) chains will
# linger until the next boot after disabling ufw.
for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-logging-output ufw$type-user-logging-forward ufw$type-user-limit-accept ufw$type-user-limit ufw$type-user-input ufw$type-user-forward ufw$type-user-output ; do
if [ "$UFW_INIT_DEBUG" = "yes" ]; then
echo "$exe $flag $c" >&2
$exe -X $c || true
else
$exe -X $c 2>/dev/null || true
fi
done
}
ufw_start() {
out=""
if iptables -L ufw-user-input -n >/dev/null 2>&1 ; then
echo "Firewall already started, use 'force-reload'"
return 0
fi
if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
for m in $IPT_MODULES
do
modprobe $m || true
done
if [ "$MANAGE_BUILTINS" = "yes" ]; then
flush_builtins
fi
execs="iptables"
# IPv6 setup
if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
if ip6tables -L INPUT -n >/dev/null 2>&1; then
execs="$execs ip6tables"
else
out="${out}\nProblem loading ipv6 (skipping)"
fi
else
if ip6tables -L INPUT -n >/dev/null 2>&1; then
# IPv6 support disabled but available in the kernel, so
# default DROP and accept all on loopback
delete_chains 6 || error="yes"
ip6tables -P INPUT DROP || error="yes"
ip6tables -P OUTPUT DROP || error="yes"
ip6tables -P FORWARD DROP || error="yes"
# delete these first so don't add multiple rules
ip6tables -D INPUT -i lo -j ACCEPT 2>/dev/null || true
ip6tables -D OUTPUT -o lo -j ACCEPT 2>/dev/null || true
ip6tables -A INPUT -i lo -j ACCEPT || error="yes"
ip6tables -A OUTPUT -o lo -j ACCEPT || error="yes"